/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dnbW.uiW.dbW;

import controller.StartUp;
import dnbW.dbW.*;
import dnbW.dbW.sucxentdbW.SucxentDatabaseEngine;
import java.io.*;
import java.util.List;
import java.util.ArrayList;

/**
 * This is the class in charge of loading and saving databases from the hard drive
 */
public class DiskStorageManager {
    public static final String DB_LIST_FILE = StartUp.DB_LIST_PATH;
    public static List<String> dbNames;
    
    public static void loadDatabases(DatabaseList dbList) {
        File file = new File(DB_LIST_FILE);
        
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            dbNames = new ArrayList<String>();
            String line = null;
            line = reader.readLine();
            while (line != null && !line.trim().isEmpty()) {
                String dbName = line.trim();
                line = reader.readLine();

                String hostName = line.trim();
                line = reader.readLine();
                
                String listDBName = dbName +"@"+ hostName;
                
                dbNames.add(listDBName);

                String userName = line.trim();
                line = reader.readLine();

                String password = line.trim();
                line = reader.readLine();

                DatabaseInfo db = new DatabaseInfo(hostName, dbName, userName, password);
                dbList.addElement(db);

                DatabaseEngine engine = new SucxentDatabaseEngine(db);
                db.setEngine(engine);
                db.connect();
            }
            reader.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void saveDatabases(DatabaseList dbList) {
        File file = new File(DB_LIST_FILE);
        if (file.exists()) {
            file.delete();
        }

        try {
            PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
            for (DatabaseInfo db : dbList.getDatabases()) {
                
                writer.println(db.getDbName());
                writer.println(db.getHostName());
                writer.println(db.getUserName());
                writer.println(db.getPassword());
            }
            writer.flush();
            writer.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public static DatabaseInfo loadSelectedDatabase(String selectedDBName) {
        File file = new File(DB_LIST_FILE);
        
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            line = reader.readLine();
            while (line != null && !line.trim().isEmpty()) {
                if(line.equals(selectedDBName))
                {   
                    String dbName = line.trim();
                    
                    line = reader.readLine();
                    String hostName = line.trim();
                    
                    line = reader.readLine();
                    String userName = line.trim();
                        
                    line = reader.readLine();
                    String password = line.trim();
                    
                    DatabaseInfo db = new DatabaseInfo(hostName, dbName, userName, password);

                    DatabaseEngine engine = new SucxentDatabaseEngine(db);
                    db.setEngine(engine);
                    db.connect();
                    
                    return db;                                       
                }
                else
                {
                    line = reader.readLine();
                }
            }
            
            reader.close();
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }  catch (IOException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return null;
    }
}
